<!--审核-->
<template>
  <div class="need-submit-box pending-task"
       v-loading="loading"
       element-loading-customClass="ne-loading"
       element-loading-background="rgba(255, 255, 255, 0.6)">
   <h2 class="ne-title-v2">审核</h2>
   <el-row style="width: 50%;margin: 0 auto;">
        <tc-form ref="tempForm" :rules="rules" :form="Form1" @submitFun="validate" @closeFun="closeFun"></tc-form>
   </el-row>
  </div>
</template>
<script>
import CustOrderService from "@/api/order/CustOrderService";
import CommonUtil from "@/utils/common"
import TcForm from "@/components/public/TcForm";
import {mapState} from 'vuex';
import refreshList from "@/mixins/custorder/refreshList";
export default {
  name: "Audit",
  mixins: [refreshList],
  props: {
    ODKInfo: {
      type: Object
    },
    CTOInfo: {
      type: Object
    }
  },
  data() {
    return {
      loading: false,
      audUserList:[],
      rules:{
        audResultFlag:[
          { required: true, message: '请选择审核结果', trigger: 'change' }
        ],
        audComment:[
          { required: false, message: '请填写审核意见', trigger: 'blur' }
        ],
        audCompany:[
          { required: true, message: '请选择集团', trigger: 'change' }
        ],
        sendAudName:[
          { required: true, message: '请选择名称', trigger: 'change' }
        ],
        backReason:[
          { required: true, message: '请选择退回原因', trigger: 'change' }
        ]
      },
      Form1:{
        // 审核标识
        audResultFlag:{
          type:'select',
          cName:'审核结果',
          cValue:'',
          required:true,
          show:true,
          span:18,
          opt:[{label:'同意',value:1},{label:'不同意',value:0}]
        },
        // 审核标识
        audFlag:{
          type:'checkbox',
          cName:'是否终审',
          cValue:true,
          show:true,
          span:6,
        },
        // 审核意见
        audComment:{
          type:'textarea',
          cName:'审核意见',
          required:false,
          cValue:'',
          show:true,
          span:24,
        },
        //审核单位
        audCompany:{
          type:'select',
          cName:'审核单位',
          cValue:'',
          required:true,
          pair:'',
          opt:[],
          show:false,
          span:24,
        },
        //审核人
        sendAudName:{
          type:'select',
          cName:'审核人',
          cValue:'',
          required:true,
          opt:[],
          show:false,
          span:24,
        },
        //退回原因
        backReason:{
          type:'select',
          cName:'退回原因',
          cValue:'',
          opt:[{label:'录入原因',value:18107},
            {label:'其他原因',value:8996640}],
          show:false,
          span:24,
        }
      }
    }
  },
  mounted() {

  },
  computed:{
    ...mapState('user',['user'])
  },
  created() {
    this.gryDemandAudUserList();
  },
  components: {
    TcForm
  },
  methods: {
    async gryDemandAudUserList(){
      // this.loading = true;
      let {taskId,tacheId}=this.ODKInfo,
          {custSoNumber,batchNbr,custOrderId,custId}=this.CTOInfo,
          params = {taskId,tacheId,custSoNumber,batchNbr,custOrderId,custId};
      const result = await CustOrderService.gryDemandAudUserList(params);
      this.audUserList = this.handelUserListFun(result.meta.staffs.staff);
      // this.loading = false;
    },
    handelUserListFun(list){
      let k = {},
          auditList = [];
      list.forEach((item)=>{
        if(k[item.orgId]){
          let {staffId,staffName} = item;
          auditList[k[item.orgId]-1].staff.push({value:staffId+'',label:staffName});
        }else{
          let {orgId,orgName,staffId,staffName} = item;
          this.Form1.audCompany.opt.push({label:orgName+'',value:orgId});
          auditList.push({
            orgId:orgId,orgName:orgName,staff:[{value:staffId+'',label:staffName}]
          });
          k[item.orgId]=auditList.length;
        }
      });
      return auditList
    },
    closeFun(){
      this.$emit('update:show',false)
    },
    validate(){
      // if(!this.Form1.sendAudName.show){
      //   this.submitFun();
      //   return
      // }
      
      this.$refs['tempForm'].$refs['TcForm'].validate((valid) => {
        
        if (valid) {
          // 成功
          this.submitFun();
        } else {
          
          return false;
        }
      });
    },
    async submitFun(){
      this.loading = true;
      let {orderAuditId,taskId,tacheId,activityId,processInstId} = this.ODKInfo;
      let {custId,batchNbr,custSoNumber,custOrderId,custOrderType,orderItemId} = this.CTOInfo;
      let nepara = CommonUtil.paramsFun2(this.Form1);
      nepara.sendAudUserId = nepara.sendAudName;
      this.Form1.sendAudName.opt.forEach((item)=>{
        if(item.value == nepara.sendAudUserId){
          nepara.sendAudName = item.label
        }
      });
      /*******************************/
      delete nepara['audCompany'];
      let ldata = {
        orderAuditUserFlag:false,
        handleOrgId:this.user.orgId,
        priority:0,
        sendEmailFlag:0,
        isUpdateCustWorkFlag:0,
        staffId:this.user.staffId,
        staffName:this.user.staffName,
        audUserId:this.user.staffId,
        audName:this.user.staffName,
        oneOrmany:'many'
      };
      let allParam = {orderItemId,activityId,processInstId,custId,orderAuditId,taskId,tacheId,batchNbr,custSoNumber,custOrderId,custOrderType,...ldata,...nepara};
      
      let result = null
      if(this.CTOInfo.batchNbr=='0'){
        // 需求单
        result = await CustOrderService.saveAudit(allParam);
      }else {
        // 订单
        result = await CustOrderService.saveAuditOrder(allParam);
      }
      this.loading = false;
      try {
        if(result.meta.resultCode == 0){
          this.$message({
            type: 'success',
            message: '保存成功!'
          });
          this.eventDoSearch();
          this.$emit('update:show',false);
        }else{
          this.$message({
            showClose: true,
            message: result.meta.resultMsg,
            type: 'error'
          });
        }
      } catch {
        this.$message({
          showClose: true,
          message: 'code:'+result.code+'  msg:'+result.msg,
          type: 'error'
        });
      }
    }
  },
  watch: {
    'Form1.audResultFlag.cValue':{
      handler(newName) {
        let fav = {show:false,cValue:''},
            trv = {show:true};
        if(newName==1){
          /**
           * 审核意见:同意
           * */
          
          this.rules.audComment[0].required = false;
          if(this.Form1.audFlag.cValue){
            Object.assign(this.Form1.audCompany,fav);
            Object.assign(this.Form1.sendAudName,fav);
            Object.assign(this.Form1.backReason,fav);
          }else{
            Object.assign(this.Form1.audCompany,trv);
            Object.assign(this.Form1.sendAudName,trv);
            Object.assign(this.Form1.backReason,fav);
          }
        }else if(newName===0) {
          /**
           * 审核意见:不同意
           * */
          this.rules.audComment[0].required = true;
          Object.assign(this.Form1.audCompany,fav);
          Object.assign(this.Form1.sendAudName,fav);
          Object.assign(this.Form1.backReason,trv);
        }else{
          // 清空
          Object.assign(this.Form1.audCompany,fav);
          Object.assign(this.Form1.sendAudName,fav);
          Object.assign(this.Form1.backReason,fav);
        }
      }
    },
    'Form1.audFlag.cValue':{
      handler(newName) {
        let fav = {show:false,cValue:''},
            trv = {show:true};
        if(this.Form1.audResultFlag.cValue){
          if(newName){
            Object.assign(this.Form1.audCompany,fav);
            Object.assign(this.Form1.sendAudName,fav);
          }else {
            Object.assign(this.Form1.audCompany,trv);
            Object.assign(this.Form1.sendAudName,trv);
          }
        }
      }
    },
    'Form1.audCompany.cValue':{
      handler(newName) {
        this.audUserList.forEach(item=>{
          if(item.orgId==newName){
            this.Form1.sendAudName.opt=item.staff;
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.ne-title-v2{
  font-weight: 600;
  margin-left: .12rem;
}
::v-deep .el-loading-spinner{
  i,.el-loading-text{
    color: rgba(0,82,217,.7);
  }
}
</style>
